/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projeto;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import projeto.classes.ModeloTipoVeiculo;
import projeto.classes.ModeloVeiculo;
import projeto.classes.TipoVeiculoAcessaDados;
import projeto.classes.VeiculoAcessaDados;


public class Veiculo_ConsultaTodos extends javax.swing.JDialog {

    private ArrayList<ModeloTipoVeiculo> listaTipo;
    
    /**
     * Creates new form Veiculo_ConsultaTodos
     */
    public Veiculo_ConsultaTodos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        carregarTipo();
        carregarVeiculos();
    }
    
    private void carregarTipo() {
        try {
            TipoVeiculoAcessaDados tipoDao = new TipoVeiculoAcessaDados();
            listaTipo = tipoDao.consultaTodos();
        } catch (Exception ex) {
           JOptionPane.showMessageDialog(this, "Erro ao consultar " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
       }
    }
    
    private String obterNomeTipo(int tipoId) {
        String nomeTipo = "";
        
        try {
            if (listaTipo.size() > 0) {
                for (int i = 0; i < listaTipo.size(); i++) {
                    ModeloTipoVeiculo tipo = listaTipo.get(i);
                    
                    if (tipo.getId() == tipoId) {
                        nomeTipo = tipo.getTipoVeiculo();
                        return nomeTipo;
                    }
                }
            } else {
                JOptionPane.showMessageDialog(this, "Não há tipos de veiculos cadastrados.", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
            }
            
        } catch (Exception ex) {
           JOptionPane.showMessageDialog(this, "Erro ao consultar " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
       }
        
        return nomeTipo;       
    }
    
    private void carregarVeiculos() {
       VeiculoAcessaDados vDao = new VeiculoAcessaDados();
       ArrayList<ModeloVeiculo> lista;

       try {
           lista = vDao.consultaTodos();
           DefaultTableModel modelo = (DefaultTableModel) tblAnimais.getModel();

           if (lista.size() > 0 && listaTipo.size() > 0) {
               for (int i = 0; i < lista.size(); i++) {
                   ModeloVeiculo v = lista.get(i);
                   String nomeTipo = obterNomeTipo(v.getTipoVeiculoId());
                   
                   String combustivel;                  
                   switch(v.getTipoCombustivel()) {
                        case 0:
                            combustivel = "Gasolina";
                            break;
                        case 1:
                            combustivel = "Álcool";
                            break;
                        case 2:
                            combustivel = "Gás";
                            break;
                        case 3:
                            combustivel = "Diesel";
                            break;
                        default:
                            combustivel = "";
                                   
                    }

                   modelo.addRow(new Object[] {
                       v.getId(),
                       v.getNomeMarca(),
                       nomeTipo,
                       v.getAno(),
                       combustivel,
                       v.getCor(),
                       v.getTerceirizado() != 0
                   });
               }
           } else {
               JOptionPane.showMessageDialog(this, "Não há veiculos cadastrados", "Entrega Gado", JOptionPane.INFORMATION_MESSAGE);
               tblAnimais.setEnabled(false);
           }
       } catch (Exception ex) {
           JOptionPane.showMessageDialog(this, "Erro ao consultar " + ex.getMessage(), "Entrega Gado", JOptionPane.ERROR_MESSAGE);
       }
   }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblAnimais = new javax.swing.JTable();
        btnFechar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tblAnimais.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Marca", "Tipo", "Ano", "Combustivel", "Cor", "Terceirizado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblAnimais);

        btnFechar.setText("Fechar");
        btnFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 585, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(259, 259, 259)
                .addComponent(btnFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(btnFechar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFecharActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnFecharActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Veiculo_ConsultaTodos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Veiculo_ConsultaTodos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Veiculo_ConsultaTodos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Veiculo_ConsultaTodos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Veiculo_ConsultaTodos dialog = new Veiculo_ConsultaTodos(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFechar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblAnimais;
    // End of variables declaration//GEN-END:variables
}
